package behavioral_pattern.command_pattern.test2;

import java.io.*;
import java.util.ArrayList;

public class FileUtil {
    //将命令集合写入日志文件
    public static void writeCommands(ArrayList commands){
        try {
            FileOutputStream fileOutputStream = new FileOutputStream("config.log");
            //创建对象输出流用于将对象写入到文件中
            ObjectOutputStream outputStream = new ObjectOutputStream(new BufferedOutputStream(fileOutputStream));
            //将对象写入文件
            outputStream.writeObject(commands);
            outputStream.close();
        }catch (Exception e){
            System.out.println("命令保存失败!");
        }
    }

    public static ArrayList readCommands(){
        try {
            FileInputStream fileInputStream = new FileInputStream("config.log");
            ObjectInputStream objectInputStream = new ObjectInputStream(new BufferedInputStream(fileInputStream));

            ArrayList commands = (ArrayList) objectInputStream.readObject();

            objectInputStream.close();
            return commands;

        }catch (Exception e){
            System.out.println("命令读取失败!");
        }
        return null;
    }
}
